package com.mediacloud.app.newsmodule.fragment.ugc.detail

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chinamcloud.project.shanshipin.utils.tiktok.TikTokView
import com.mediacloud.app.assembly.views.BlurTransformation
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.yoake.media.player.utils.cache.PreloadManager
import java.util.*

class LiteAvDetailAdapter(val context: Context, val items: MutableList<ArticleItem>) : PagerAdapter() {

    private val mViewPool: MutableList<View> = ArrayList()
    override fun isViewFromObject(view: View, `object`: Any) = view === `object`
    override fun getCount() = items.size
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        var view: View? = null
        if (mViewPool.size > 0) { //取第一个进行复用
            view = mViewPool[0]
            mViewPool.removeAt(0)
        }
        val viewHolder = if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_tik_tok, container, false)
            ViewHolder(view)
        } else {
            view.tag as ViewHolder
        }
        val item: ArticleItem = items[position]
        PreloadManager.getInstance(context).addPreloadTask(item.tipVideoAdress, position)
        val drawable = ContextCompat.getDrawable(context, R.drawable.smallvideodetailbg)
        val option = RequestOptions()
            .transform(BlurTransformation(context))
            .placeholder(drawable)
            .error(drawable)
            .override(100, 100)
        val option2 = RequestOptions()
            .placeholder(drawable)
            .error(drawable)
        Glide.with(context).load(item.tipImage).apply(option).into(viewHolder.mIvBg)
        Glide.with(context).load(item.tipImage).apply(option2).into(viewHolder.mThumb)
        viewHolder.mPosition = position
        container.addView(view)
        return view!!
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val itemView = `object` as View
        container.removeView(itemView)
        val item: ArticleItem = items[position]
        PreloadManager.getInstance(container.context).removePreloadTask(item.tipVideoAdress)
        //保存起来用来复用
        mViewPool.add(itemView)
    }

    /**
     * 借鉴ListView item复用方法
     */
    class ViewHolder internal constructor(itemView: View) {
        var mPosition = 0
        var mIvBg: ImageView
        var mTikTokView: TikTokView = itemView.findViewById(R.id.tiktok_View)
        var mThumb: ImageView = itemView.findViewById(R.id.iv_thumb)
        var mPlayerContainer: FrameLayout

        init {
            mIvBg = mTikTokView.findViewById(R.id.iv_bg)
            mPlayerContainer = itemView.findViewById(R.id.container)
            itemView.tag = this
        }
    }
}